package net.sharpf2.api.world;

import java.awt.Point;
import net.sharpf2.sfcore.client.Toolkit;
import net.sharpf2.api.Globals;

/**
 *
 * @author Rick van Biljouw
 */
public class Space {
    
    /**
     * Turns a 3d-space coordinate into 2d-space
     * @param   x   X Coord.
     * @param   y   Y Coord.
     * @param   z   Z Coord.
     * @return  Point Coordination with x and y.
     */
    public static Point get2DCoordinate(int x, int y, int z) {
        Toolkit render = Globals.CLIENT.getToolkit();
        float _z = (render.getViewport().getZOffset() + ((int) (render.getViewport().getZX() * x + render.getViewport().getZY() * z + render.getViewport().getZZ() * y)));
        if ((_z >= render.getZNear()) && (_z <= render.getZFar())) {
            int _x = (int) (render.getScaleX() * ((int) render.getViewport().getXOffset() + ((int) (render.getViewport().getXX() * x + render.getViewport().getXY() * z + render.getViewport().getXZ() * y))) / _z);
            int _y = (int) (render.getScaleY() * ((int) render.getViewport().getYOffset() + ((int) (render.getViewport().getYX() * x + render.getViewport().getYY() * z + render.getViewport().getYZ() * y))) / _z);

            if ((_x >= render.getAbsoluteX1()) && (_x <= render.getAbsoluteX2()) && (_y >= render.getAbsoluteY1()) && (_y <= render.getAbsoluteY2())) {
                return new Point((int) (_x - render.getAbsoluteX1()), (int) (_y - render.getAbsoluteY1()));
            }
        }
        return new Point(-1, -1);
    }   
    
    /**
     * Turns a 3d-space coordinate into 2d-space
     * @param   x   X Coord.
     * @param   y   Y Coord.
     * @return  Point Coordination with x and y.
     */
    public static Point get2DCoordinate(int x, int y) {
        return get2DCoordinate(x, y, Ground.getHeight(x,y));
    }        
    
}
